package com.kj.platformlogistics.home.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.cy.tablayoutniubility.FragPageAdapterVp2;
import com.cy.tablayoutniubility.TabAdapter;
import com.cy.tablayoutniubility.TabLayoutScroll;
import com.cy.tablayoutniubility.TabMediatorVp2;
import com.cy.tablayoutniubility.TabViewHolder;
import com.kj.platformlogistics.R;
import com.kj.platformlogistics.base.BaseActivity;
import com.kj.platformlogistics.home.EventBusBean;
import com.kj.platformlogistics.home.fragment.SendingFragment;
import com.kj.platformlogistics.home.fragment.SolicilationedFragment;
import com.kj.platformlogistics.util.GlideEngine;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class SolicilationedActivity extends BaseActivity implements View.OnClickListener {
    private TabLayoutScroll tab1;
    private ViewPager2 viewpager;
    private ImageView _barIvRight,ivSelectAll;
    private TextView tvTakePicture;
    private Boolean isEdit=false;
    private LinearLayout llBottom;

    private Boolean isSelectAll=false;//是否全选

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, SolicilationedActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_wait_solicitation;
    }

    @Override
    protected void initView() {
        setTitleText("已揽件");
        setRightImg(R.mipmap.icon_camera_black);
        tvTakePicture=findViewById(R.id.tvTakePicture);
        llBottom=findViewById(R.id.llBottom);
        ivSelectAll=findViewById(R.id.ivSelectAll);
        _barIvRight=findViewById(R.id._barIvRight);
        _barIvRight.setOnClickListener(this);
        llBottom.setOnClickListener(this::onClick);
        ivSelectAll.setOnClickListener(this::onClick);
        tvTakePicture.setOnClickListener(this::onClick);
        tab1=findViewById(R.id.tab1);
        viewpager=findViewById(R.id.viewpager);
        FragPageAdapterVp2<String> fragmentPageAdapter = new FragPageAdapterVp2<String>(this) {

            @Override
            public Fragment createFragment(String bean, int position) {
                switch (position) {
                    case 0://全部
                        return   SolicilationedFragment.newInstance(0,1);
                    case 1://待付款
                        return   SolicilationedFragment.newInstance(1,1);
                }
                return null;
            }

            @Override
            public void bindDataToTab(TabViewHolder holder, int position, String bean, boolean isSelected) {
                TextView textView = holder.getView(R.id.tv);
                if (isSelected) {
                    textView.setTextColor(0xff333333);
                    textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    textView.setTextSize(17);
                } else {
                    textView.setTextColor(0xff999999);
                    textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    textView.setTextSize(14);
                }
                textView.setText(bean);
            }

            @Override
            public int getTabLayoutID(int position, String bean) {
                return R.layout.tab_item;
            }
        };
        TabAdapter<String> tabAdapter = new TabMediatorVp2<String>(tab1, viewpager).setAdapter(fragmentPageAdapter);
        List<String> list = new ArrayList<>();
        list.add("平台下单");
        list.add("客户下单");
        fragmentPageAdapter.add(list);
        tabAdapter.add(list);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_RIGHT_IMG;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvTakePicture:
                PictureSelector.create(SolicilationedActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .selectionMode(PictureConfig.SINGLE)
                        .isCamera(true)
                        .compressQuality(100)
                        .loadImageEngine(GlideEngine.createGlideEngine()) // 请参考Demo GlideEngine.java
                        .forResult(PictureConfig.REQUEST_CAMERA);
                break;
            case R.id._barIvRight:
                if (!isEdit){
                    _barIvRight.setImageResource(R.mipmap.icon_cancle);
                    llBottom.setVisibility(View.VISIBLE);
                    EventBusBean bean=new EventBusBean();
                    bean.setCode(10);
                    EventBus.getDefault().post(bean);
                }else {
                    _barIvRight.setImageResource(R.mipmap.icon_camera_black);
                    llBottom.setVisibility(View.GONE);
                    EventBusBean bean=new EventBusBean();
                    bean.setCode(11);
                    EventBus.getDefault().post(bean);
                }
                isEdit=!isEdit;
                break;
            case R.id.ivSelectAll:
                if (!isSelectAll){
                    ivSelectAll.setImageResource(R.mipmap.icon_selected_dot);
                   EventBusBean busBean=new EventBusBean();
                   busBean.setCode(12);
                   EventBus.getDefault().post(busBean);
                }else {
                    ivSelectAll.setImageResource(R.mipmap.icon_unselected_dot);
                    EventBusBean busBean=new EventBusBean();
                    busBean.setCode(13);
                    EventBus.getDefault().post(busBean);
                }
                isSelectAll=!isSelectAll;
                break;
        }
    }

    private String path="";
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.REQUEST_CAMERA:
                    // 结果回调
                    List<LocalMedia> cameraList = PictureSelector.obtainMultipleResult(data);
                    if(cameraList.size()>0){
                        path=cameraList.get(0).getRealPath();
                    }
                    EventBusBean busBean=new EventBusBean();
                    busBean.setCode(18);
                    busBean.setMsg(path);
                    EventBus.getDefault().post(busBean);
                    break;
                default:
                    break;
            }
        }
    }
}
